/**
 * ---------------------------
 * Time: 2017/5/15 23:04.
 * Author: Cangshi
 * View: http://palerock.cn
 * ---------------------------
 */
var Plane = Main.createConfigClass({
    default: {
        defaultConfig: function () {
            var superConfig = this.doSuper('defaultConfig', arguments);
            superConfig.name = 'PlaneObject';
            return superConfig;
        }
    },
    parentClass: {
        class: CanvasInnerObject,
        args: [{}]
    }
});

var MyPlane = Main.createConfigClass({
    default: {
        defaultVar: function () {
            return this.doSuper('defaultVar', arguments)
                .extend({
                    playerTouch: {
                        distance: 0,
                        deg: 0,
                        openTouch: function () {
                            var _this = this;
                            this.listenerHandle = window.listenerManage.addListener('touchstart touchmove', function (e) {
                                switch (e.type) {
                                    case 'touchstart':
                                        _this.touchPoint.x = e.touches[0].clientX;
                                        _this.touchPoint.y = e.touches[0].clientY;
                                        break;
                                    case 'touchmove':
                                        _this.onTouch(e);
                                        break;
                                    default:
                                        break;
                                }
                            });
                        },
                        closeTouch: function () {
                            window.listenerManage.removeListener(this.listenerHandle);
                        },
                        onTouch: function (e) {
                            var _this = this;
                            e.preventDefault();
                            var movePoint = new Cielar.Point({
                                x: e.touches[0].clientX, y: e.touches[0].clientY
                            });
                            _this.distance = _this.touchPoint.getDistanceFromAnotherPoint(movePoint) * _this.sensitivity;
                            _this.deg = _this.touchPoint.getDegFromAnotherPoint(movePoint);
                            _this.superEntity.doTouch();
                            _this.touchPoint.x = e.touches[0].clientX;
                            _this.touchPoint.y = e.touches[0].clientY;
                        },
                        onMouseMove: function (e) {
                            console.log(e);
                            var _this = this;
                            e.preventDefault();
                            var movePoint = new Cielar.Point({
                                x: e.touches[0].clientX, y: e.touches[0].clientY
                            });
                            _this.distance = _this.touchPoint.getDistanceFromAnotherPoint(movePoint) * _this.sensitivity;
                            _this.deg = _this.touchPoint.getDegFromAnotherPoint(movePoint);
                            _this.superEntity.doTouch();
                            _this.touchPoint.x = e.touches[0].clientX;
                            _this.touchPoint.y = e.touches[0].clientY;
                        }
                    }
                });
        },
        defaultConfig: function () {
            var superConfig = this.doSuper('defaultConfig', arguments);
            superConfig.runningTrack = {
                speed: 0,
                direction: 135,
                acceleration: 0,
                maxSpeed: 20,
                doRunningOnce: function () {
                    this.speed = 0;
                },
                doChanges: function () {
                },
                onRunningInterceptor: function (e) {
                    var isInterceptor = e.centrePoint.isInRect(0, 0, window.innerWidth, window.innerHeight);
                    e.preventRunning = !isInterceptor;
                }
            };
            superConfig.playerTouch = {
                sensitivity: 1.5
            };
            return superConfig;
        },
        onConfigured: function () {
            this.doSuper('onConfigured', arguments);
            this.playerTouch = {};
            this.playerTouch.expand(this.config.playerTouch);
        },
        onInitFinish: function () {
            this.doSuper('onInitFinish', arguments);
            this.startTouch();
        }
    },
    defaultPrototype: {
        doTouch: function () {
            var doChangesConfig = {
                distance: this.playerTouch.distance,
                deg: this.playerTouch.deg
            };
            this.runningTrack.doChanges = function () {
                this.speed = doChangesConfig.distance;
                this.direction = doChangesConfig.deg;
                // 让该方法只能执行一次
                delete this.doChanges;
            }
        },
        startTouch: function () {
            this.playerTouch.superEntity = this;
            this.playerTouch.touchPoint = new Cielar.Point();
            this.playerTouch.openTouch();
        }
    },
    parentClass: {
        class: Plane,
        args: [{}]
    }
});